package com.ruoyi.web.controller.user;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.user.domain.UserInfo;
import com.ruoyi.user.domain.UserInfoDtl;
import com.ruoyi.user.service.IUserService;
import com.ruoyi.web.controller.system.SysProfileController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("客户端用户信息管理")
@RestController
@RequestMapping("/client/user")
public class UserController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(SysProfileController.class);
	
	 @Autowired
	 private IUserService iUserService;
	 
	    @ApiOperation("获取用户列表")
	    @GetMapping("/list")
	    public AjaxResult userList(){
	    
	        List<UserInfo> userList = iUserService.listUserInfo();
	        return AjaxResult.success(userList);
	    }
	    
	    
	    @ApiOperation("获取用户详情")
	    @GetMapping("/getUserDetail")
	    public AjaxResult getUserDetail(Long userId){
	    
	    	UserInfoDtl userInfoDtl = iUserService.getUserDetail(userId);
	        return AjaxResult.success(userInfoDtl);
	    }
	    
	    /**
	     * 保存头像
	     */
	    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
	    @PostMapping("/updateAvatar")
	    @ResponseBody
	    public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file)
	    {
	        SysUser currentUser = ShiroUtils.getSysUser();
	        try {
	        
	            if (!file.isEmpty()) {
	            	//返回图片上传  url   相对路径
	                String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
	                currentUser.setAvatar(avatar);
//	                if (userService.updateUserInfo(currentUser) > 0) {
//	                
//	                    ShiroUtils.setSysUser(userService.selectUserById(currentUser.getUserId()));
//	                    return success();
//	                }
	            }
	            return error();
	        }
	        catch (Exception e)
	        {
	            log.error("修改头像失败！", e);
	            return error(e.getMessage());
	        }
	    }

}
